package com.cloudfun.campusshare.service.handler;

import com.cloudfun.campusshare.common.constant.CashAdvanceStatus;
import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.PayPlatform;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.dto.WithdrawHisDTO;
import com.cloudfun.campusshare.common.model.dto.kjt.KjtNotifyBaseDTO;
import com.cloudfun.campusshare.common.model.dto.kjt.KjtSubMerchantEntrustWithdrawalNotifyDTO;
import com.cloudfun.campusshare.common.model.dto.kjt.KjtSubMerchantWithdrawalCashNotifyDTO;
import com.cloudfun.campusshare.entity.CashAdvanceEntity;
import com.cloudfun.campusshare.entity.SubMerchantEntity;
import com.cloudfun.campusshare.repo.CashAdvanceEntityRepo;
import com.cloudfun.campusshare.service.KjtSubMerchantService;
import com.cloudfun.campusshare.service.WithdrawHisService;
import com.cloudfun.campusshare.util.AmountUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;


/**
 * Created by Huqin on 2020/8/17
 */
@Slf4j
@Component
public class KjtEntrustWithdrawStatusNotifyHandler implements KjtNotifyHandler<KjtSubMerchantEntrustWithdrawalNotifyDTO> {

    @Autowired
    private KjtSubMerchantService subMerchantService;

    @Autowired
    private CashAdvanceEntityRepo cashAdvanceEntityRepo;

    @Autowired
    private WithdrawHisService withdrawHisService;

    @Override
    public String notifyType() {
        return "entrust_withdrawal_status_sync";
    }

    @Override
    public Class<KjtSubMerchantEntrustWithdrawalNotifyDTO> notifyMessageType() {
        return KjtSubMerchantEntrustWithdrawalNotifyDTO.class;
    }

    @Override
    public boolean handle(KjtNotifyBaseDTO notifyMsg) {
        KjtSubMerchantEntrustWithdrawalNotifyDTO notifyDTO = (KjtSubMerchantEntrustWithdrawalNotifyDTO) notifyMsg;
        log.info("提现回调通知:{}",notifyMsg);
        SubMerchantEntity subMerchantEntity = subMerchantService.getEntityByIdentityId(notifyDTO.getPayeeId()).orElseThrow(() -> new BusinessException(Code.NODATA));

        WithdrawHisDTO withdrawHisDTO = new WithdrawHisDTO();
        withdrawHisDTO.setSubMerchantId(subMerchantEntity.getId());
        withdrawHisDTO.setUserId(subMerchantEntity.getUserId());
        withdrawHisDTO.setOutTradeNo(notifyDTO.getInnerTradeNo());
        int withdrawAmount = AmountUtil.strAmount2Int(notifyDTO.getWithdrawalAmount());
        withdrawHisDTO.setWithdrawalAmount(withdrawAmount);
        withdrawHisDTO.setWithdrawStatus(notifyDTO.getWithdrawalStatus());
        withdrawHisDTO.setFailReason(notifyDTO.getFailReason());
        withdrawHisDTO.setWithdrawTime(LocalDateTime.parse(notifyDTO.getGmtWithdrawal(), KjtSubMerchantWithdrawalCashNotifyDTO.GMT_WITHDRAWAL_FORMATTER));
        withdrawHisService.create(withdrawHisDTO);
        return true;
    }
}
